<?php
define ( 'HDOM_TYPE_ELEMENT', 1 );
define ( 'HDOM_TYPE_COMMENT', 2 );
define ( 'HDOM_TYPE_TEXT', 3 );
define ( 'HDOM_TYPE_ENDTAG', 4 );
define ( 'HDOM_TYPE_ROOT', 5 );
define ( 'HDOM_TYPE_UNKNOWN', 6 );
define ( 'HDOM_QUOTE_DOUBLE', 0 );
define ( 'HDOM_QUOTE_SINGLE', 1 );
define ( 'HDOM_QUOTE_NO', 3 );
define ( 'HDOM_INFO_BEGIN', 0 );
define ( 'HDOM_INFO_END', 1 );
define ( 'HDOM_INFO_QUOTE', 2 );
define ( 'HDOM_INFO_SPACE', 3 );
define ( 'HDOM_INFO_TEXT', 4 );
define ( 'HDOM_INFO_INNER', 5 );
define ( 'HDOM_INFO_OUTER', 6 );
define ( 'HDOM_INFO_ENDSPACE', 7 );
define ( 'DEFAULT_TARGET_CHARSET', 'UTF-8' );
define ( 'DEFAULT_BR_TEXT', "\r\n" );
define ( 'DEFAULT_SPAN_TEXT', " " );
define ( 'MAX_FILE_SIZE', 600000 );
/**
 * 从文件得到html dom类
 * 
 * @param 文件url路径 $url        	
 * @param string $use_include_path        	
 * @param string $context        	
 * @param 偏移位置 $offset        	
 * @param 组大长度 $maxLen        	
 * @param string $lowercase        	
 * @param string $forceTagsClosed        	
 * @param string $target_charset        	
 * @param string $stripRN        	
 * @param string $defaultBRText        	
 * @param string $defaultSpanText        	
 * @return boolean|simple_html_dom
 */
function file_get_html($url, $use_include_path = false, $context = null, $offset = -1, $maxLen = -1, $lowercase = true, $forceTagsClosed = true, $target_charset = DEFAULT_TARGET_CHARSET, $stripRN = true, $defaultBRText = DEFAULT_BR_TEXT, $defaultSpanText = DEFAULT_SPAN_TEXT) {
	$dom = new simple_html_dom ( null, $lowercase, $forceTagsClosed, $target_charset, $stripRN, $defaultBRText, $defaultSpanText );
	$contents = file_get_contents ( $url, $use_include_path, $context, $offset );
	if (empty ( $contents ) || strlen ( $contents ) > MAX_FILE_SIZE) {
		return false;
	}
	$dom->load ( $contents, $lowercase, $stripRN );
	return $dom;
}

/**
 * 从字符串得到html dom 对象
 * 
 * @param unknown $str        	
 * @param string $lowercase        	
 * @param string $forceTagsClosed        	
 * @param string $target_charset        	
 * @param string $stripRN        	
 * @param string $defaultBRText        	
 * @param string $defaultSpanText        	
 * @return boolean|simple_html_dom
 */
function str_get_html($str, $lowercase = true, $forceTagsClosed = true, $target_charset = DEFAULT_TARGET_CHARSET, $stripRN = true, $defaultBRText = DEFAULT_BR_TEXT, $defaultSpanText = DEFAULT_SPAN_TEXT) {
	$dom = new simple_html_dom ( null, $lowercase, $forceTagsClosed, $target_charset, $stripRN, $defaultBRText, $defaultSpanText );
	if (empty ( $str ) || strlen ( $str ) > MAX_FILE_SIZE) {
		$dom->clear ();
		return false;
	}
	$dom->load ( $str, $lowercase, $stripRN );
	return $dom;
}
/**
 * 打印html dom 树方法
 * 
 * @param unknown $node        	
 * @param string $show_attr        	
 * @param number $deep        	
 */
function dump_html_tree($node, $show_attr = true, $deep = 0) {
	$node->dump ( $node );
}
class simple_html_dom {
	public $root = null;
	public $nodes = array ();
	public $callback = null;
	public $lowercase = false;
	public $original_size;
	public $size;
	protected $pos;
	protected $doc;
	protected $char;
	protected $cursor;
	protected $parent;
	protected $noise = array ();
	protected $token_blank = "\t\r\n";
	protected $token_equal = '=/>';
	protected $token_slash = "/>\r\n\t";
	protected $token_attr = '>';
	public $_charset = '';
	public $_target_charset = '';
	protected $default_br_text = "";
	public $default_span_text = "";
	protected $self_closing_tags = array (
			'img' => 1,
			'br' => 1,
			'input' => 1,
			'meta' => 1,
			'link' => 1,
			'hr' => 1,
			'base' => 1,
			'embed' => 1,
			'spacer' => 1 
	);
	protected $block_tags = array (
			'tr'=>array('tr'=>1,'td'=>1,'th'=>1),
			'th'=>array('th'=>1),
			'td'=>array('td'=>1),
			'li'=>array('li'=>1),
			'dt'=>array('dt'=>1,'dd'=>1),
			'dd'=>array('dd'=>1,'dt'=>1),
			'dl'=>array('dd'=>1,'dt'=>1),
			'p'=>array('p'=>1),
			'nobr'=>array('nobr'=>1),
			'b'=>array('b'=>1),
			'option'=>array('option'=>1)
	);
	function __construct($str=null,$lowercase=true,$forceTagsClosed=true,$target_charset=DEFAULT_TARGET_CHARSET,$stripRN=true,$defaultBRText=DEFAULT_BR_TEXT,$defaultSpanText=DEFAULT_SPAN_TEXT){
		if($str){
			if(preg_match("/^http:\/\//i", $str) || is_file($str)){
				$this->load_file($str);
			}else{
				$this->load($str,$lowercase,$stripRN,$defaultBRText,$defaultSpanText);
			}			
		}
		if(!$forceTagsClosed){
			$this->optional_closing_array=array();
		}
		$this->_target_charset=$target_charset;
	}
	function __destruct(){
		$this->clear();
	}
	
	function load($str,$lowercase=true,$stripRN=true,$defaultBRText=DEFAULT_BR_TEXT,$defaultSpanText=DEFAULT_SPAN_TEXT){
		global $debugObject;
		$this->prepare($str,$lowercase,$stripRN,$defaultBRText,$defaultSpanText);
		$this->remove_noise("'<!--(.*?)->'is");
		$this->remove_noise("'<!\[CDATA(\[.*?\])\]>'is");
		$this->remove_noise("'<\s*script[^>]*[^/]>(.*?)<\s*/\s*script\s*>'is");
		$this->remove_noise();
		$this->remove_noise();
		$this->remove_noise();
		$this->remove_noise();
		$this->remove_noise();
		
		while($this->parse());
		$this->root->_[HDOM_INFO_END]=$this->cursor;
		$this->parse_charset();
		return $this;
	}
	
	
	function clear(){
		
	}
	function prepare(){
		
	}
}



?>